package com.mj.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.mj.model.system.Dept;
import com.mj.model.system.Menu;

/**
 * 通用工具类方法
 *@ClassName:CommonUtil.java
 *@author dsc
 *@date 2017年7月20日
 *@Description TODO
 *
 */
@Component
public class CommonUtil {
	
	
	/**
     * 递归遍历当前用户角色对应的菜单
     * @param menuList  菜单集合
     * @param parentId  父id
     * @return
     */
//    public List<TreeNode> treeMenuList( List<Menu> menuList, int parentId) {
//		 List<TreeNode> childMenu = new  ArrayList<TreeNode>();  
//	       for (Menu m : menuList) {
//	    	   TreeNode tn = new TreeNode();
//	    	   tn.setTitle(m.getMenuName());
//	    	   tn.setHref(m.getMenuUrl());
//	    	   tn.setIcon(m.getIconUrl());
//	           int menuId = m.getMenuId() ;
//	           int pid = m.getParentMenuId();
//	           if (parentId == pid) {
//	        	   List<TreeNode> c_node = treeMenuList(menuList, menuId);  
//	        	   tn.setChildren(c_node);
//	               childMenu.add(tn);  
//	           }  
//	       }  
//	       return childMenu;  
//	   }
    
    /**
     * 菜单封装treegrid
     * @param menuList
     * @param parentId
     * @return
     */
    public List<Map<String,Object>> treeMapList( List<Menu> menuList, long parentId) {
		 List<Map<String,Object>> childMenu = new  ArrayList<Map<String,Object>>();  
	       for (Menu m : menuList) {
	    	   Map<String,Object> tn = new HashMap<String,Object>();
	    	   tn.put("title", m.getMenuName());
	    	   tn.put("href",m.getMenuUrl());
	    	   tn.put("id",new Long(m.getMenuId()).intValue());
	    	   tn.put("pid", m.getParentMenuId());
	    	   tn.put("pids", m.getParentMenuIds());
	    	   tn.put("icon",m.getIconUrl());
	    	   tn.put("isiframe",m.getIsiframe());
	    	   tn.put("permission",m.getPermission());
	           long menuId = m.getMenuId();
	           long pid = m.getParentMenuId();
//	           if(m.getIsLeaf().equals("N")){
//	        	   tn.put("state", "closed");
//	           }
	           if (parentId == pid) {
	        	   List<Map<String,Object>> c_node = treeMapList(menuList, menuId);  
	        	   tn.put("children",c_node);
	               childMenu.add(tn);
	           }  
	       }  
	       return childMenu;  
	   }
    
    
    /**
     * 部门封装treegrid
     * @param menuList
     * @param parentId
     * @return
     */
    public List<Map<String,Object>> deptMapList( List<Dept> deptList, long parentId) {
		 List<Map<String,Object>> childMenu = new  ArrayList<Map<String,Object>>();  
	       for (Dept m : deptList) {
	    	   Map<String,Object> tn = new HashMap<String,Object>();
	    	   tn.put("title", m.getDeptName());
 	    	   tn.put("id",m.getDeptId());
 	    	   tn.put("num", m.getDeptNum());
 	    	   tn.put("status", m.getStatus());
 	    	   tn.put("pid", m.getDeptPid());
	           long deptId = m.getDeptId();
	           long pid = m.getDeptPid();
	           if (parentId == pid) {
	        	   List<Map<String,Object>> c_node = deptMapList(deptList, deptId);  
	        	   tn.put("children",c_node);
	               childMenu.add(tn);
	           }  
	       }  
	       return childMenu;  
	   }
    
    /**
     * easyui tree格式封装
     * @param menuList
     * @param parentId
     * @return
     */
    public List<Map<String,Object>> deptList( List<Dept> menuList, long parentId) {
		 List<Map<String,Object>> childMenu = new  ArrayList<Map<String,Object>>();  
	       for (Dept m : menuList) {
	    	   Map<String,Object> tn = new HashMap<String,Object>();
	    	   tn.put("text", m.getDeptName());
	    	   tn.put("id",m.getDeptId());
	           long menuId = m.getDeptId();
	           long pid = m.getDeptPid();
	           if (parentId == pid) {
	        	   List<Map<String,Object>> c_node = deptList(menuList, menuId);  
	        	   tn.put("children",c_node);
	               childMenu.add(tn);
	           }  
	       }  
	       return childMenu;  
	   }
    
    
    public List<Map<String,Object>> treeList( List<Menu> menuList, long parentId) {
		 List<Map<String,Object>> childMenu = new  ArrayList<Map<String,Object>>();  
	       for (Menu m : menuList) {
	    	   Map<String,Object> tn = new HashMap<String,Object>();
	    	   tn.put("text", m.getMenuName());
	    	   tn.put("id",new Long(m.getMenuId()).intValue());
	           long menuId = m.getMenuId();
	           long pid = m.getParentMenuId();
	           if (parentId == pid) {
	        	   List<Map<String,Object>> c_node = treeList(menuList, menuId);  
	        	   tn.put("children",c_node);
	               childMenu.add(tn);
	           }  
	       }  
	       return childMenu;  
	   }
}
